package com.yanjiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanjiali.webmagic.GetDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjiali.mapper.ThirdPartyMapper;
import com.yanjiali.entity.ThirdParty;
import com.yanjiali.service.ThirdPartyService;

import java.util.List;

/**
* @Package: com.yanjiali.service.impl
* @Author: yanjiali
* @Created: 2025/5/8 15:49
*/
@Service
public class ThirdPartyServiceImpl extends ServiceImpl<ThirdPartyMapper, ThirdParty> implements ThirdPartyService{

    @Override
    public void saveThirdPartyList(List<ThirdParty> thirdPartyList) {
        for(ThirdParty thirdParty : thirdPartyList) {
            LambdaQueryWrapper<ThirdParty> lqw = new LambdaQueryWrapper<>();
            lqw
                    .eq(ThirdParty::getTitle, thirdParty.getTitle())
                    .or()
                    .eq(ThirdParty::getUrl, thirdParty.getUrl());

            List<ThirdParty> list = list(lqw);
            if(list != null && list.size() > 0) {   //说明这个数据已经存在了，即时已经使用了布隆过滤器，但是重新运行还是回来重复数据
                continue;
            }

            boolean save = save(thirdParty);
            if(!save) {
                throw new RuntimeException("第三方数据--" + thirdParty.getTitle() + "存储失败");
            }
        }
    }
}
